package net.rock.eshop.controller;

import net.rock.eshop.entity.Address;
import net.rock.eshop.service.AddressService;
import net.rock.eshop.service.UserService;
import net.rock.eshop.utils.UserIdGetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/address")
public class AddressController {
    @Autowired
    UserService userService;

    @Autowired
    private AddressService addressService;
    @Autowired
    private UserIdGetter userIdGetter;


    @GetMapping("/show")
    public List<Address> showAddress(@CookieValue("sessionID")String sessionId) {
        int userId = userIdGetter.getUserIdBySessionId(sessionId);
        return addressService.showAddress(userId);
    }

    @PostMapping("/add")
    public void add(@CookieValue(value = "sessionID") String sessionId,@RequestBody Address address){
        int userId = userIdGetter.getUserIdBySessionId(sessionId);
        address.setUserId(userId);
        addressService.add(address);
    }

    @PostMapping("/del")
    public void del(@CookieValue(value = "sessionID") String sessionId,@RequestBody Address address){
        int userId = userIdGetter.getUserIdBySessionId(sessionId);
        address.setUserId(userId);
        addressService.del(address);
    }

    @PostMapping("/edit")
    public void edit(@CookieValue(value = "sessionID") String sessionId,@RequestBody Address address){
        int userId = userIdGetter.getUserIdBySessionId(sessionId);
        address.setUserId(userId);
        addressService.edit(address);
    }
}
